var Session = require('../../vendor/qcloud-weapp-client-sdk/lib/session.js');
var config = require('../../config.js');
var commons = require('../../commons.js');
var qcloud = require('../../vendor/qcloud-weapp-client-sdk/index');
var register = require('../../template/register/register.js');
var utils = require('../../vendor/qcloud-weapp-client-sdk/lib/utils.js');
var index = {

  /**
   * 页面的初始数据
   */
  data: {
    dataOver: false, //下划线
    page: 1, //分页
    navCuttid: 0,
    showPageZb: false,
    collArrayData: [],
    myZhuBoListData: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.indexListData();
  },
  //点击跳转主播个人详情页
  clickGuanZhuGoGr: function(e) {
    var id = e.currentTarget.id;
    var userId = this.data.myZhuBoListData[id].id;
    wx.navigateTo({
      url: '../center/center?userId=' + userId,
    })

  },
  // 跳转直播页面
  goToDetails: function (e) {
    var id = e.currentTarget.id;
    var detailID = this.data.collArrayData[id].room_id;
    wx.navigateTo({
      url: '../details/details?ia=' + detailID,
    })
  },
  // 主播请求列表
  zhuBoClick: function() {
    this.setData({
      collArrayData: [],
      page: 1,
      navCuttid: 1,
      showPageZb: true
    })
    // this.myZhuBoList();
  },
  // 视频请求列表
  shiPinClick: function() {
    this.setData({
      myZhuBoListData: [],
      navCuttid: 0,
      page: 1,
      showPageZb: false
    })
    this.indexListData();
  },
  // 我的关注视频请求
  indexListData: function() {
    wx.showLoading({
      title: '请稍后',
    })
    var that = this;
    qcloud.request({
      login: false,
      url: config.service.collArrayUrl,
      data: {
        page: that.data.page
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function(res) {
        var arr = that.data.collArrayData;
        arr = arr.concat(that._formatData(res.data.data.data));
        if (that.data.collArrayData.length !== 0) {
          if (res.data.data.data.length == 0) {
            // 分页没有数据了，显示提示框
            that.setData({
              dataOver: true
            })
          } else {
            that.setData({
              page: that.data.page + 1,
              collArrayData: arr,
            });
          }
        } else {
          that.setData({
            dataOver: true,
            collArrayData: arr,
            page: that.data.page + 1,
          })
        }
      },
      fail: function(res) {
        utils.showError('请求出错')
      },
      complete: function() {
        setTimeout(function() {
          wx.hideLoading();
          wx.stopPullDownRefresh();
        }, 500)
      }
    });

  },
  //我的关注列表
  myZhuBoList: function() {
    wx.showLoading({
      title: '请稍后',
    })
    var that = this;
    qcloud.request({
      login: false,
      url: config.service.collRoomUrl,
      data: {
        page: that.data.page
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function(res) {
        var arr = that.data.myZhuBoListData;
        arr = arr.concat(that._formatData(res.data.data.data));
        if (that.data.myZhuBoListData.length !== 0) {
          if (res.data.data.data.length == 0) {
            // 分页没有数据了，显示提示框
            that.setData({
              dataOver: true
            })
          } else {
            that.setData({
              page: that.data.page + 1,
              myZhuBoListData: arr,
            });
          }
        } else {
          that.setData({
            myZhuBoListData: arr,
            page: that.data.page + 1,
          })
        }
      },
      fail: function(res) {
        utils.showError('请求出错')
      },
      complete: function() {
        setTimeout(function() {
          wx.hideLoading();
          wx.stopPullDownRefresh();
        }, 500)
      }
    });

  },
  //时间转换
  _formatData: function (dataArray) {
    var that = this;
    for (var i in dataArray) {
      dataArray[i].created = commons.dateTools.timestampToDataDayTwo(dataArray[i].created);
      dataArray[i].click = commons.dateTools.wanNumZh(dataArray[i].click);
    }
    return dataArray;
  },
  // 视频取消取消关注
  clickQuXiaoGz: function(e) {
    var id = e.currentTarget.id;
    var that = this;
    wx.showModal({
      title: '提示',
      content: '您确定要取消关注吗',
      success(res) {
        if (res.confirm) {
          that.quXiaoGz(id, 0);
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  //直播取消关注
  clickZhuBo: function(e) {
    var id = e.currentTarget.id;
    var that = this;
    wx.showModal({
      title: '提示',
      content: '您确定要取消关注吗',
      success(res) {
        if (res.confirm) {
          that.quXiaoGz(id, 1);
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  // 取消关注
  quXiaoGz: function(id, a) {
    var that = this;
    // if (a == 0) {
    //   var b = that.data.collArrayData[id].room_id
    // } else {
    //   var b = that.data.myZhuBoListData[id].id
    // }
    var b = that.data.collArrayData[id].id
    qcloud.request({
      login: false,
      url: config.service.collDelUrl,
      data: {
        id: b
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function(res) {
        if (res.data.data.code == 0) {
          utils.showError('取消成功');
          that.setData({
            page: 1,
            collArrayData: [],
            myZhuBoListData: []
          })
        } else {
          utils.showError('操作失败,请重试');
        }
      },
      fail: function(res) {
        utils.showError('请求出错')
      },
      complete: function() {
        setTimeout(function() {
          wx.hideLoading();
          wx.stopPullDownRefresh();
          if (a == 0) {
            that.indexListData();
          } else {
            that.myZhuBoList();
          }
        }, 500)
      }
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    this.setData({
      page: 1,
      collArrayData: [],
      myZhuBoListData: []

    })
    this.indexListData();
    // this.myZhuBoList();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    if (this.data.navCuttid == 0) {
      this.indexListData();
    } else {
      this.myZhuBoList();
    }
  },

  /**
 * 用户点击右上角分享
 */
  onShareAppMessage: function () {
    return {
      title: '小城市live',
      path: '/pages/index/index',
      success: function () {
        utils.showSuccess("转发成功");
      }
    };
  }
};
var data = Object.assign(index.data, register.data);
var page = Object.assign(index, register);
page.data = data;
Page(page);